﻿using System;
using System.Web;
using System.Web.Caching;
using System.Text;

namespace Soft000.Common
{
    /// <summary>
    /// Cache操作类
    /// </summary>
    public static class Cache
    {
        #region 添加
        /// <summary>
        /// 简单创建/修改Cache
        /// </summary>
        /// <param name="strCatcheName">Cache名称</param>
        /// <param name="objValue">Cache值</param>
        /// <param name="iExpires">有效期，秒数</param>
        public static void Add(string strCatcheName, Object objValue, int iExpires, int nPriority)
        {
            //默认缓存时间是相对的
            Add(strCatcheName, objValue, iExpires, false, nPriority);
        }
        /// <summary>
        /// 简单创建/修改Cache
        /// </summary>
        /// <param name="strCatcheName">Cache名称</param>
        /// <param name="objValue">Cache值</param>
        /// <param name="iExpires">有效期，秒数</param>
        public static void Add(string strCatcheNaem, Object objValue, int iExpires)
        {
            //默认缓存时间是相对的
            Add(strCatcheNaem, objValue, iExpires, false);
        }

        /// <summary>
        /// 简单创建/修改Cache
        /// </summary>
        /// <param name="strCatcheName">Cache名称</param>
        /// <param name="objValue">Cache值</param>
        /// <param name="iExpires">有效期，秒数</param>
        /// <param name="absolute">true:绝对时间，对像缓存指定时间后删除；false:相对象时间，如果时间为10秒，则在该对象最后一次访问10秒后删除</param>
        public static void Add(string strCatcheName, Object objValue, int iExpires, bool absolute)
        {
            Add(strCatcheName, objValue, iExpires, absolute, 4);
        }

        /// <summary>
        /// 简单创建/修改Cache
        /// </summary>
        /// <param name="strCacheName">Cache名称</param>
        /// <param name="objValue">Cache值</param>
        /// <param name="iExpires">有效期，秒数</param>
        /// <param name="absolute">true:绝对时间，对像缓存指定时间后删除；false:相对象时间，如果时间为10秒，则在该对象最后一次访问10秒后删除</param>
        /// <param name="priority">保留优先级，1最不会被清除，6最容易被内存管理清除（1:NotRemovable；2:High；3:AboveNormal；4:Normal；5:BelowNormal；6:Low）</param>
        public static void Add(string strCacheName, Object objValue, int iExpires,bool absolute,int priority)
        {
            TimeSpan ts = new TimeSpan(0, 0, iExpires);
            CacheItemPriority cachePriority;
            switch (priority)
            {
                case 6:
                    cachePriority = CacheItemPriority.Low;
                    break;
                case 5:
                    cachePriority = CacheItemPriority.BelowNormal;
                    break;
                case 4:
                    cachePriority = CacheItemPriority.Normal;
                    break;
                case 3:
                    cachePriority = CacheItemPriority.AboveNormal;
                    break;
                case 2:
                    cachePriority = CacheItemPriority.High;
                    break;
                case 1:
                    cachePriority = CacheItemPriority.NotRemovable;
                    break;
                default:
                    cachePriority = CacheItemPriority.Default;
                    break;
            }
            if (absolute)
            {
                HttpContext.Current.Cache.Insert(strCacheName, objValue, null, DateTime.Now.Add(ts), System.Web.Caching.Cache.NoSlidingExpiration, cachePriority, null);
            }
            else
            {
                HttpContext.Current.Cache.Insert(strCacheName, objValue, null,System.Web.Caching.Cache.NoAbsoluteExpiration,ts, cachePriority, null);
            }
        }
        #endregion

        #region 获取
        /// <summary>
        /// 简单读Cache对象的值
        /// </summary>
        /// <param name="strCacheName">Cache名称</param>
        /// <returns>Cache对像</returns>
        public static Object Get(string strCacheName)
        {
            return HttpContext.Current.Cache[strCacheName];
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除Cache对象
        /// </summary>
        /// <param name="strCacheName">Cache名称</param>
        public static void Del(string strCacheName)
        {
            HttpContext.Current.Cache.Remove(strCacheName);
        }
        #endregion
    }
}
